import axios from 'axios';
//判断用户名是否重复
export async function repeatName(username) {
    let resp = await axios.post("/api/users/usernameExist", {
        username
    });
    console.log("resp", resp);
    return resp.data;
}
//获取登录的用户信息
export async function getUserLogin(username, password) {
    let resp = await axios.post("/api/users/login", {
        username,
        password
    });
    console.log("getUserLogin", resp.data);
    // var token = resp.headers.authorization;
    // //将token保存到浏览器的localStorage中
    // if(token){
    //     localStorage.setItem("token",token);
    // }

    return resp.data;
}
//验证token的值正不正确
export async function userVerify() { //把token传到数据库 得到resp数据库的返回值
    let resp = await axios.get("/api/users/verify/whoami")
    console.log(resp);
    return resp.data;
}
//注销
export function loginOut() {
    localStorage.removeItem("token"); //删除token
}

//查询所有用户
export async function getAllUsers( page, pageSize,type, value) {
    let resp = await axios.get("/api/users", {
        params: {
            current: page, //第几页 1234
            pageSize, //每页显示条数
            type,
            value
        }
    });
    console.log("resp", resp);
    return resp.data;
}
//查询对应的用户
export async function getOneUsers(type, value) {
    let resp = await axios.get("/api/users", {
        params: {
            type,
            value
        }
    });
    console.log("resp", resp);
    return resp.data;
}


//新增用户
export async function getAddUsers({ username, password, telephone, email, privilege,picture }) {
    let resp = await axios.post("/api/users/", {
        username, password, telephone, email,privilege,picture
    });
    console.log("resp", resp);
    return resp.data;
}

//通过id删除一个用户
export async function deleteById(_id) {
    await axios.delete("/api/users/" + _id);
}

//通过id查询一个用户
export async function findById(_id) {
    let resp = await axios.get("/api/users/" + _id);
    console.log("resp", resp);
    return resp.data;
}

//通过id修改一个用户信息
export async function changeById(object) {
    let { _id,
        username,
        password,
        telephone,
        email,
        picture,
        privilege,
        createTime, } = object;
    let resp = await axios.put("/api/users/" + _id, {
        username,
        password,
        telephone,
        email,
        picture,
        privilege,
        createTime,
    });
    console.log("resp", resp);
    return resp.data;
}
// export async function changeById( _id,
//     username,
//     password,
//     telephone,
//     email,
//     picture,
//     privilege,
//     createTime, ) {
//     let resp = await axios.put("/api/users/" + _id, {
//         username,
//         password,
//         telephone,
//         email,
//         picture,
//         privilege,
//         createTime,
//     });
//     console.log("resp", resp);
//     return resp.data;
// }
